- Create a config folder in your module’s base directory. All con guration YAMLs should be in a subfolder of
- Create a folder named install in the config folder. Con guration YAMLs in this folder will be imported on module installation.
- In the install folder, create a contact.form.contactus.yml to store the YAML de nition of the contact form, Contact Us:
-
- We will de ne the con guration of a contact form based on the contact.schema. yml le provided by the Contact module:
langcode: en status: true dependences: {} id: contactus label: 'Contact Us' recipients:
- webmaster@example.com reply: '' weight: 0
The con guration entry is based on a schema de nition, which we will cover in Chapter 9, Con uration Management – Deploying in Drupal 8. The langcode, status, and dependencies are the required con guration management keys.
The id is the contact form’s machine name and the label is the human display name. The recipients key is a YAML array of valid e-mail addresses. The reply key is a string of text for the Auto-reply eld. And, nally, the weight de nes the form’s weight in the administrative list.
- Visit Extend and enable your module to import the con guration item.
- The Contact Us form will now be located on the Contact forms overview page,located under Structure:
- Create a mymodule.install le in the module’s base directory. We will create an update hook to set a reply message for the contact form.
- We will create a function called mymodule_update_8001() that will be read by the update system and make our con guration changes:<?php
/** * Update "Contact Us" form to have a reply message.
*/ function mymodule_update_8001() {
$contact_form = \Drupal\contact\Entity\ ContactForm::load('contact_us');
$contact_form->setReply(t('Thank you for contacting us, we will reply shortly'));
$contact_form->save(); }
This function uses the entity’s class to load our configuration entity object. It loads contact_us, which our module has provided, and sets the reply property to a new value.
9. Visit /update.php in your browser to run the Drupal’s database update system:
10. Review the Contact Us form settings and verify that the reply message has been set.
How it works…
Drupal’s moduler_installer service, provided through \Drupal\Core\Extension\ ModuleInstaller, ensures that configuration items de ned in the module’s config folder are processed on installation. When a module is installed, the config.installer service, provided through \Drupal\Core\Config\ConfigInstaller, is called to process the module’s default configuration.
In the event, the config.installer service makes an attempt to import the configuration from the install folder that already exists and an exception will be thrown. Modules cannot provide changes made to the existing configuration through static YAML definitions.
Since modules cannot adjust configuration objects through static YAML de nitions provided
to Drupal, modules can utilize the database update system to modify the configuration. Drupal utilizes a schema version for modules. The base schema version for a module is 8000. Modules can provide update hooks in the form of hook_update_N, where N represents the next schema version. When Drupal’s updates are run, they will execute the proper update hooks and update the module’s schema version.Configuration objects are immutable by default. In order to edit a configuration, a mutable object needs to be loaded through the con guration factory service.
Configuration subdirectories
There are three directories that the configuration management system will inspect in a module’s config folder, which are as follows:
f install f optional f schema
The install folder specifies the configuration that will be imported. If the configuration object exists, the installation will fail.
The optional folder contains the configuration that will be installed if the following conditions are met:
f The configuration does not already exist f It is a configuration entity
f Its dependencies can be metIf any one of the conditions fail, the configuration will not be installed, but it will not halt the module’s installation process.
The schema folder provides definitions of configuration object definitions. This uses YAML definitions to structure configuration objects and is covered in depth in Chapter 9, Configuration Management.
- We will de ne the con guration of a contact form based on the contact.schema. yml le provided by the Contact module: